package com.xiaoq.matrix.minipro.repository.impl;

import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.matrix.minipro.entity.ShopMiniProEntity;
import com.xiaoq.matrix.minipro.entity.ShopMiniProVersionEntity;
import com.xiaoq.matrix.minipro.vo.MiniProAuthorizerInfo;

import io.jsonwebtoken.lang.Collections;

@Repository
public class ShopMiniProRepositoryImpl {
	@Autowired
	private Datastore mongo;
	
	@Autowired
	private IdGenerator idGenerator;
	
	private static final Logger LOG = LoggerFactory.getLogger(ShopMiniProRepositoryImpl.class);
	
	/**
	 * 根据appId获取认证信息
	 * @param appId
	 * @return
	 */
	public MiniProAuthorizerInfo getAuthorizerInfoByAppId(String appId) {
		return this.getByAppId(appId).getAuthorizerInfo();
	}
	
	/**
	 * 添加商户号的商户秘钥的方法
	 * @param shopId
	 * @param appId
	 * @param appSecret
	 * @return
	 */
	public boolean addSecret(String appId,String appSecret){
		ShopMiniProEntity e = this.getByAppId(appId);
		if(e!=null){
			e.setAppSecret(appSecret);
			mongo.save(e);
			LOG.info("appId:"+appId+"添加账号成功！");
			return true;
		}
		return false;
		
	}
	
	public void saveAuthorizerInfo(String appId, MiniProAuthorizerInfo authInfo, String userId, String merchantId, String shopId, String shopType,String type) {
		ShopMiniProEntity e = this.getByAppId(appId);
		ShopMiniProEntity e2 = this.getByAppShopIdAndType(shopId, type);
		if (e == null&&e2==null) {
			LOG.info("新增一条认证记录appId"+appId+"公众号类型"+type);
			e = new ShopMiniProEntity();
			e.setAppId(appId);
			e.setAuthorizerInfo(authInfo);
			e.setId(idGenerator.nextId());
			e.setMerchantId(merchantId);
			e.setShopId(shopId);
			e.setShopType(shopType);
			e.setType(type);
			// TODO 其他属性待完善
			mongo.save(e);
			return;
		}
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		q.field("appId").equal(appId);
		UpdateOperations<ShopMiniProEntity> op = mongo.createUpdateOperations(ShopMiniProEntity.class);
		if(authInfo!=null){
			op.set("authorizerInfo", authInfo);
		}
		if(merchantId!=null){
			op.set("merchantId", merchantId);
		}
		if(shopId!=null){
			op.set("shopId", shopId);
		}
		if(shopType!=null){
			op.set("shopType", shopType);
		}
		if(type!=null){
			op.set("type", type);
		}
		mongo.update(q, op);
	}
	
	public ShopMiniProEntity saveShopMiniProEntity(ShopMiniProEntity shopMiniPro){
		mongo.save(shopMiniPro);
		return shopMiniPro;
	} 
	
	public ShopMiniProEntity getByShopId(String shopId,String type) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		List<ShopMiniProEntity> list = q.field("shopId").equal(shopId).field("type").equal(type).asList();
		if (Collections.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	
	public ShopMiniProEntity getByMerchantId(String merchantId,String type) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		List<ShopMiniProEntity> list = q.field("merchantId").equal(merchantId).field("type").equal(type).asList();
		if (Collections.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	public ShopMiniProEntity getByAppShopIdAndType(String shopId,String type) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		q.field("shopId").equal(shopId);
		q.field("type").equal(type);
		List<ShopMiniProEntity> list = q.asList();
		if (Collections.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	
	public ShopMiniProEntity getByAppId(String appId) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		List<ShopMiniProEntity> list = q.field("appId").equal(appId).asList();
		if (Collections.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	public void saveLasterVersion(String appId, ShopMiniProVersionEntity version) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		q.filter("appId", appId);
		UpdateOperations<ShopMiniProEntity> op = mongo.createUpdateOperations(ShopMiniProEntity.class);
		op.set("lastVersion", version);
	    mongo.update(q, op);
	}

	public void updateQrCode(String appId, String qrcode) {
		Query<ShopMiniProEntity> q = mongo.find(ShopMiniProEntity.class);
		q.filter("appId", appId);
		UpdateOperations<ShopMiniProEntity> op = mongo.createUpdateOperations(ShopMiniProEntity.class);
		op.set("qrcode", qrcode);
	    mongo.update(q, op);
	}
	
}
